package ide.ccf.ccproject.pages;

import java.io.File;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorActionBarContributor;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.FileEditorInput;

import ide.ccf.ccproject.common.EditorInputWatcher;
import ide.ccf.ccproject.editors.ProjectFileEditor;
import ide.ccf.ccproject.editors.ProjectFileEditorActionContributor;
import ide.ccf.ccproject.pages.embedded.ProjectFileEditorView;
import ide.ccf.ccproject.pages.embedded.ProjectFileEditorViewListener;


/**
 * CCF项目文件可视化设置页面
 *
 */
public class ProjectFileCommonPage extends FormPage implements SelectionListener, ProjectFileEditorViewListener
{
	private ProjectFileEditorView projectFileEditorView = null;
	private ProjectFileEditor m_editor;
	
	/**
	 * 构造函数
	 * @param editor
	 */
	public ProjectFileCommonPage(ProjectFileEditor editor)
	{
		super(editor, "ide.ccf.ccproject.pages.ProjectFileCommonPage", "设置");
		m_editor = editor;
	}

	/**
	 * 激活
	 */
	public void setActive(boolean active)
	{
		super.setActive(active);
	}
	
	/**
	 * 选中
	 */
	public void widgetSelected(SelectionEvent e)
	{

	}
	
	/**
	 * 默认
	 */
	public void widgetDefaultSelected(SelectionEvent e)
	{
		
	}
	
	/**
	 * 数据是否过时
	 */
	public boolean isDirty()
	{
		return (projectFileEditorView != null ? projectFileEditorView.isDirty() : false);		
	}
	
	/**
	 * 保存
	 */
	public void doSave(IProgressMonitor monitor)
	{
		if (projectFileEditorView == null)
			return;
		
		FileEditorInput fin = (FileEditorInput)getEditorInput();
		
		if (!projectFileEditorView.save()) 
		{
    		MessageDialog.openError(getSite().getShell(), "保存文件失败", "无法写入文件到：" + fin.getFile().getLocation().toOSString() + "\n确认文件权限为可写。");
		}
	}
	
	/**
	 * 是否可用
	 * @param actId
	 * @return
	 */
	public boolean isActionEnabled(int actId) 
	{
		return (projectFileEditorView != null ? projectFileEditorView.isActionEnabled(actId) : false);
	}
	
	/**
	 * 切换
	 * @param actId
	 */
	public void triggerAction(int actId) 
	{
		if (projectFileEditorView == null)
			return;
		
		projectFileEditorView.triggerAction(actId);
	}

	/**
	 * 内容
	 */
	protected void createFormContent(IManagedForm managedForm)
	{
		Composite parent = managedForm.getForm().getBody();
		FormToolkit toolkit = managedForm.getToolkit();
		FileEditorInput fin = (FileEditorInput)getEditorInput();
		String fileName = fin.getFile().getLocation().toOSString();
		File file = new File(fileName);
		boolean readable = file.exists() && file.canRead();
		
		if (!readable) 
		{
			EditorInputWatcher.createMissingFileInfo(parent, fileName);
			return;
		}
		
		GridLayout layout = new GridLayout(1, false);
		parent.setLayout(layout);
		
		createProjectFileEditorView(toolkit, parent);
		projectFileEditorView.showModel(fileName);
	}

	/**
	 * 创建编辑器视图
	 * @param toolkit
	 * @param parent
	 */
	private void createProjectFileEditorView(FormToolkit toolkit, Composite parent)
	{
		projectFileEditorView = new ProjectFileEditorView(parent, SWT.EMBEDDED);
		projectFileEditorView.addProjectFileEditorViewListener(this);
		projectFileEditorView.setLayoutData(new GridData(600, 500));
		toolkit.adapt(projectFileEditorView);
		toolkit.paintBordersFor(projectFileEditorView);
	}

	/**
	 * 重载
	 */
	public void reload() 
	{
		if(projectFileEditorView != null)
			projectFileEditorView.reload();
	}

	/**
	 * 获取内容
	 * @return
	 */
	public String contents() 
	{
		return (projectFileEditorView != null ? projectFileEditorView.contents() : "");
	}

	/**
	 * 状态改变
	 */
	public void changed() 
	{
		m_editor.editorDirtyStateChanged();		
	}
	
	/**
	 * 改变
	 */
	public void actionChanged(int id) 
	{
		IEditorActionBarContributor cont = getEditorSite().getActionBarContributor();          
		
		if (cont instanceof ProjectFileEditorActionContributor) 
		{
			ProjectFileEditorActionContributor pecont = (ProjectFileEditorActionContributor)cont;
			pecont.updateAction(id);
		}
	}

	/**
	 * 设置焦点
	 */
	public void setFocus() 
	{
		super.setFocus();
		
		if (projectFileEditorView != null)
			projectFileEditorView.setFocus();
	}
}
